package Util;

import io.vertx.reactivex.ext.web.RoutingContext;

public class WebUtil {

    public static <T> T getJsonBodyToModel(RoutingContext rc, Class<T> type) {
        String body = rc.getBody().toString();

        return JsonUtil.FromJson(body, type);
    }

    public static <T> void responseJsonByModel(RoutingContext rc, T model) {
        rc.response()
                .putHeader("content-type", "application/json; charset=utf-8")
                .end(JsonUtil.ToJson(model));
    }

    public static boolean VerifySign(RoutingContext rc) {
        if (!rc.request().headers().contains("timestamp")
                || !rc.request().headers().contains("sign")) {
            return false;
        }
        try {
            long timestamp = Long.parseLong(rc.request().getHeader("timestamp"));
            String sign = rc.request().getHeader("sign");

            if (!Sign.VerifySign(timestamp, sign)) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
